const path = require("path");
const HtmlPlugin = require("html-webpack-plugin");

module.exports = {
  mode: "development",
  entry: {
    app: path.join(__dirname, "src/index.js"),
  },

  output: {
    filename: "boundel.js",
    path: path.join(__dirname, "dist"),
  },
  devServer: {
    hot: true,
    port: 9000,
    open: true,
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.less$/,
        use: ["style-loader", "css-loader", "less-loader"],
      },
      {
        test: /\.s(a|c)ss$/,
        use: ["style-loader", "css-loader", "sass-loader"],
      },
      // url-loader 基于file-loader 两个功能差不多，只是url-loader 比后者多了些可设置的options
      {
        test: /\.(jpg|jpeg|png|bmp|gif)$/,
        use: {
          loader: "url-loader",
          options: {
            limit: 5 * 1024, //此大小范围内的图片转换成base64，节省请求开销
            outputPath: "images", //dist下创建images文件夹，统一放图片
            name: "[name]-[hash:4].[ext]", // 图片文件名+hash值取4位数，ext:原后缀名
          },
        },
      },
      {
        test: /\.(woff|woff2|eot|svg|ttf)$/,
        use: "url-loader",
      },
      {
        test: /\.(js|jsx)$/,
        exclude: path.join(__dirname, "node_modules"),
        use: ["babel-loader"],
      },
    ],
  },
  plugins: [
    new HtmlPlugin({
      filename: "index.html",
      template: path.join(__dirname, "src/index.html"),
    }),
  ],
};
